(function($){
	/**
	 * 扩展$.fn类的方法
	 * config={
	 * 	getValue:{
	 * 		before:function(){},
	 * 		after:function(){},
	 * 		returnValue:false
	 * 	}
	 * }
	 */
	$.extFnMethod = $.extendFnMethod = function(clazz,config){
		var fn = $.fn[clazz];
		if(!fn||!config||!config){
			return ;
		}
		var methods = fn.methods;
		for(var mn in config){
			var temp = config[mn];
			if($.isFunction(temp)){
				methods[mn] = temp;
			}else{
				methods[mn] = Ctj.createInterceptor(methods[mn],temp.before,temp.after,temp.returnValue);
			}
		}
	}
	
	/**
	 * 扩展$.fn类
	 * config={
	 * 	methods:{},
	 * 	defaults:{},
	 * 	parseOptions:function(){}
	 * }
	 */
	$.extFn = $.extendFn = function(clazz,config){
		var fn = $.fn[clazz];
		if(!fn||!config){
			return ;
		}
		var methods = config.methods;
		delete config.methods;
		$.extend(true,fn.defaults,config);
		$.extFnMethod(clazz,methods);
	};
	
})(jQuery);(function($){
	$.extFnMethod("form",{
		getParams:function(jq){
			var params={};
			$(jq.get(0)).find('input[name]:enabled,textarea[name]:enabled,select[name]:enabled').each(function(){
				var $t = $(this);
				params[$t.attr("name")] = $t.val();
			});
			return params;
		}
	});
	
	
	
	
})(jQuery);(function($){
	$.extendFn("datagrid",{
		loadMsg:null,
		onLoadError:function(xhr,state){
			var error = $.ajax.parseError(xhr);
			Ctj.error(error)
		},
		methods:{
			checkData:function(jq,params){
				if(!params){
					return jq
				}
				var field = params.field,vAry=params.vAry;
				if(!field||!vAry||vAry.length==0){
					return jq;
				}
				return jq.each(function(i,target){
					var $grid =$(target); 
					var data = $grid.datagrid("getRows");
					if(!data||data.length==0){
						return;
					}
					var vMap ={};
					for(var i=0;i<vAry.length;i++){
						vMap[vAry[i]]=true;
					}
					for(var i=0;i<data.length;i++){
						var tempV = data[i][field];
						if(vMap[tempV]){
							$grid.datagrid("checkRow",i)
						}
					}
				});
			}
		}
	});
	/**
	 * 弹出窗口选择表格
	 */
	$.define("gridWin",{
		gridConfig:{
			fit:true,
			method:"GET"
		},
		modal:true,
		closed:false,
		onConfirm:function(btn){},
		onCancel:function(btn){},
		getGrid:function(){
			return this.getState().grid;
		},
		close:function(){
			this.get$t().dialog("close");
		},
		open:function(){
			this.get$t().dialog("open");
			this.uncheckAll();
		},
		getSelected:function(){
			return this.getGrid().datagrid("getSelected");
		},
		getChecked:function(){
			return this.getGrid().datagrid("getChecked");
		},
		uncheckAll:function(){
			this.getGrid().datagrid("uncheckAll");
		},
		checkData:function(field,vAry){
			var $grid =this.getGrid(); 
			$grid.datagrid("checkData",{"field":field,"vAry":vAry});
		},
		create:function($t,state){
			var me =this;
			
			state.grid =$("<div />").appendTo($t).datagrid(this.gridConfig);
			
			$t.dialog($.extend({
				buttons:[{
					text:"确认",iconCls:"icon-save",
					handler:function(){
						me.onConfirm.call(me,this,$t);
					}
				},{
					text:"取消",iconCls:"icon-back",
					handler:function(){
						if(me.onCancel.call(me,this,$t)!==false){
							me.close($t);
						}
					}
				}]
			},this));
		}
	})
	
})(jQuery);Ctj.info=function(title,msg){
	Ctj.alert(title,msg);
};

Ctj.alert=function(title,msg){
	if(!msg){
		msg = title;
		title="提示";
	}
	$.messager.alert(title,msg,"info");
};

Ctj.warn=function(title,msg){
	if(!msg){
		msg = title;
		title="警告";
	}
	$.messager.alert(title,msg,"warning");
};

Ctj.error=function(title,msg){
	if(!msg){
		msg = title;
		title="错误";
	}
	$.messager.alert(title,msg,"error");
};

Ctj.confirm=function(title,msg,fn){
	if(!fn&&Ctj.isFunction(msg)){
		fn=msg;
		msg = title;
		title="提示";
	}
	$.messager.confirm(title,msg,fn);
};

(function($){
	$.extendFn("tree",{
		methods:{
			uncheckAll:function(jq){
				return jq.each(function(){
					var $t = $(this);
					var nodes= $t.tree("getChecked");
					if(nodes&&nodes.length>0){
						$.each(nodes,function(i,node){
							$t.tree("uncheck",node.target);
						});
					}
				});
			},
			checkData:function(jq,params){
				if(!params){
					return jq
				}
				var field = params.field,vAry=params.vAry;
				if(!field||!vAry||vAry.length==0){
					return jq;
				}
				return jq.each(function(i,target){
					var $tree =$(target); 
					var roots = $tree.tree("getRoots");
					if(!roots||roots.length==0){
						return;
					}
					var vMap ={};
					for(var i=0;i<vAry.length;i++){
						vMap[vAry[i]]=true;
					}
					
					function check(nodes){
						if(!nodes||nodes.length==0){
							return;
						}
						for(var i=0;i<nodes.length;i++){
							var tempNode = nodes[i];
							var tempV = tempNode[field]; 
							if(vMap[tempV]){
								$tree.tree("check",tempNode.target);
							}
							check(tempNode.children);
						}
					}
					check(roots);
				});
			}
		},
		onLoadError:function(xhr,state){
			var error = $.ajax.parseError(xhr);
			Ctj.error(error)
		}
	});
	/**
	 * 弹出窗口选择树
	 */
	$.define("treeWin",{
		treeConfig:{
			loadFilter: function(result){
				var orgMap ={};
				Ctj.each(result,function(row,i){
					row.text = row.name;
					orgMap[row.id]=row;
				});    
				var treeData=[];
				Ctj.each(result,function(row,i){
					var pid = row.parent_id; 
					if(pid&&orgMap[pid]){
						orgMap[pid].children=orgMap[pid].children||[];
						orgMap[pid].children.push(row);
					}else{
						treeData.push(row);
					}
				});					
				return treeData;				
			}			
		},
		modal:true,
		closed:false,
		onConfirm:function(btn){},
		onCancel:function(btn){},
		getTree:function(){
			return this.getState().tree;
		},
		close:function(){
			this.get$t().dialog("close");
		},
		open:function(){
			this.get$t().dialog("open");
			this.uncheckAll();
		},
		getSelected:function(){
			return this.getTree().tree("getSelected");
		},
		getChecked:function(){
			return this.getTree().tree("getChecked");
		},
		uncheckAll:function(){
			this.getTree().tree("uncheckAll");
		},
		checkData:function(field,vAry){
			this.getTree().tree("checkData",{"field":field,"vAry":vAry});
		},
		create:function($t,state){
			var me =this;
			
			state.tree =$("<div />").appendTo($t).tree(this.treeConfig); 
			state.id = $t.attr("id");
			$t.dialog($.extend({
				buttons:[{
					text:"确认",iconCls:"icon-save",
					handler:function(){
						me.onConfirm.call(me,this,$t);
					}
				},{
					text:"取消",iconCls:"icon-back",
					handler:function(){
						if(me.onCancel.call(me,this,$t)!==false){
							me.close($t);
						}
					}
				}]
			},this));
		}
	})
})(jQuery);(function($){
	
	$.define("uploadFileWin",{
		modal:true,
		title:"上传文件",
		url:null,
		params:null,
		fileSuffix:"*",
		onSuccess:function(){},
		create:function($t,state){
			var me =this;
			var $fileForm =$('<form enctype="multipart/form-data" method="post" ><input class="easyui-filebox" name="file" style="width:300px" data-options="buttonText:\'选择\',required:true"></form>');
			state.$fileForm = $fileForm ;
			$t.css({"padding":"10px"}).append($fileForm);
			$.parser.parse($t);
			$t.dialog($.extend({
				buttons:[{
					text:"确认",iconCls:"icon-save",
					handler:function(){
						me._uploadFile();
					}
				},{
					text:"取消",iconCls:"icon-back",
					handler:function(){
						me.close($t);
					}
				}]
			},this));
		},
		close:function(){
			this.get$t().dialog("close");
		},
		_uploadFile:function(){
			var me =this;
			if(!me.url){
				Ctj.info("上传地址不能为空");
				return;
			}
			var $fileForm = me.getState().$fileForm;
			var fileName = $fileForm.find("[type='text']").val();
			var params = $.extend({
				name:fileName
			},me.params)
			var $progress=$.messager.progress({title:"提示",msg:"正在上传中"});
			$fileForm.form("submit",{
				url:me.url,
				iframe:false,
				queryParams:params,
				success:function(responseText){
					$progress.dialog("close");
					me.onSuccess.call(me,responseText,params);
				} 
			});
		}
	});
})(jQuery);